package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.ResponseCode;
import com.tbit.uqbike.constant.SysConstant;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.business.Point;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.pojo.MachineFault;
import com.tbit.uqbike.object.pojo.dto.FaultReportDTO;
import com.tbit.uqbike.object.pojo.vo.MachineFaultLogVo;
import com.tbit.uqbike.object.pojo.vo.MachineFaultPersonVo;
import com.tbit.uqbike.object.pojo.vo.MachineFaultVehicleVo;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.FaultReportService;
import com.tbit.uqbike.webmanager.dao.core.UserDao;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.OutputUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 故障上报统计
 *
 * @Author LHX
 * @ClassName FaultReportController
 * @Description
 * @date 2023/5/18 14:12
 * @Version 1.0
 */
@RestController
@RequestMapping("/faultReport")
@Slf4j
public class FaultReportController extends  BaseController {

    @Autowired
    private FaultReportService faultReportService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private UserDao userDao;

    /**
     * 故障上报统计详情
     *
     * @param faultReportDTO
     * @return
     */
    @GetMapping("/queryPageDetails")
    public Result<PageResult<MachineFaultLogVo>> queryPageDetails(FaultReportDTO faultReportDTO) {
        faultReportDTO.selectDetailParamCheck();
        PageResult<MachineFaultLogVo> pageResult = faultReportService.queryPageDetails(faultReportDTO);
        return Result.success(pageResult);
    }

    /**
     * 故障上报统计详情导出
     *
     * @param faultReportDTO
     * @return
     */
    @GetMapping("/exportDetails")
    public Result exportDetails(FaultReportDTO faultReportDTO) {
        return faultReportService.exportDetails(faultReportDTO);
    }

    /**
     * 故障上报车辆总览
     *
     * @param faultReportDTO
     * @return
     */
    @GetMapping("/queryPageVehicle")
    public Result<PageResult<MachineFaultVehicleVo>> queryPageVehicle(FaultReportDTO faultReportDTO) {
        PageResult<MachineFaultVehicleVo> pageResult = faultReportService.queryPageVehicle(faultReportDTO);
        return Result.success(pageResult);
    }

    /**
     * 故障上报车辆总览导出
     *
     * @param faultReportDTO
     * @return
     */
    @GetMapping("/exportVehicle")
    public Result exportVehicle(FaultReportDTO faultReportDTO) {
        return faultReportService.exportVehicle(faultReportDTO);
    }

    /**
     * 故障上报人员总览
     *
     * @param faultReportDTO
     * @return
     */
    @GetMapping("/queryPagePerson")
    public Result<PageResult<MachineFaultPersonVo>> queryPagePerson(FaultReportDTO faultReportDTO) {
        PageResult<MachineFaultPersonVo> pageResult = faultReportService.queryPagePerson(faultReportDTO);
        return Result.success(pageResult);
    }

    /**
     * 故障上报人员总览导出
     *
     * @param faultReportDTO
     * @return
     */
    @GetMapping("/exportPerson")
    public Result exportPerson(FaultReportDTO faultReportDTO) {
        return faultReportService.exportPerson(faultReportDTO);
    }

    /**
     * 故障上报处理记录导出
     *
     * @param faultReportDTO 。
     */
    @GetMapping("/exportRecord")
    public Result exportRecord(FaultReportDTO faultReportDTO) {
        if (org.apache.commons.lang3.StringUtils.isAllBlank(faultReportDTO.getUserCode(), faultReportDTO.getAddManPhone(),
                faultReportDTO.getOrderNO())) {
            throw new BaseException("请填写[车辆编号、联系方式、故障编号]任意项进行导出");
        }
        return faultReportService.exportRecord(faultReportDTO);
    }


    /**
     * 添加故障车辆
     *
     * @param token        令牌
     * @param files        文件
     * @param userCode     车辆编号
     * @param machineFault 故障详情
     * @param lo           经度
     * @param la           纬度
     * @param mapType      地图类型
     * @param response     响应
     * @ignoreParams request
     */
    @RequestMapping("/add")
    public void add(String token, MultipartFile[] files, String userCode, MachineFault machineFault, Double lo, Double la, @RequestParam(defaultValue = "2") Integer mapType, HttpServletRequest request, HttpServletResponse response,Integer brandId,String userPhone) {

        Map<String, Object> map = new HashMap<String, Object>();
        LoginInfo login = getLogin(request);
        try {
            Assert.notNull(machineFault.getFaults(), "异常类型不能为空");
            Assert.notNull(userCode, "用户码不能为空");
            Machine machine = machineService.getByUserCode(userCode);
            if (machine == null) {
                throw new BaseException( "设备不存在");
            }
            Point point = null;
            if (mapType != null && lo != null && la != null) {
                point = new Point(lo, la);
                point = calibrateService.commonCalibrate(point, mapType, SysConstant.ORIGINAL);
            } else {
                /**未传递赋予默认值*/
                point = new Point(0D, 0D);
            }

            /**数据组装*/
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(point.getX());
            stringBuffer.append(",");
            stringBuffer.append(point.getY());

            machineFault.setPoint(stringBuffer.toString());
            machineFault.setAddMan(login.getPhone());
            machineFault.setMachineId(machine.getMachineId());
            machineFault.setFrom(1);
            faultReportService.insert(files, machineFault, request, login, machine,brandId, userPhone);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e);
        } catch (Exception e) {
            log.error("报错了", e);
        }

        OutputUtil.renderText(request, response, map);
    }


    /**
     * 恢复正常
     * @param machineId 车辆id
     * @return Result<Object>
     */
    @GetMapping("/backToNormal")
    @OperateLog(module = ModuleEnum.TER_MANAGER, methods = MethodEnum.BACK_NORMAL)
    public Result<Object> backToNormal(Integer machineId) {
        if (Objects.isNull(machineId)){
            return Result.error("请选择车辆");
        }
        return faultReportService.backToNormal(machineId);
    }
}
